package com.ml.doctor.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.ml.doctor.R;
import com.ml.doctor.bean.BsBean;
import com.ml.doctor.bean.PatientBean;
import com.ml.doctor.bean.XXBean;
import com.ml.doctor.bean.YuYueBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.ToastUtil;
import com.ml.doctor.view.CycleImageView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SubscribeActivity extends BaseActivity {
    @BindView(R.id.iv_avatar)
    CycleImageView ivAvatar;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_sex)
    TextView txtSex;
    @BindView(R.id.txt_age)
    TextView txtAge;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.txt_bs)
    TextView txtBs;
    @BindView(R.id.txt_xx)
    TextView txtXx;
    @BindView(R.id.txt_time)
    TextView txtTime;
    YuYueBean yuYueBean;
    @BindView(R.id.layout_bottom)
    LinearLayout layoutBottom;
    int status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe_layout);
        ButterKnife.bind(this);
        setTopTitle("预约详情");
        initData();
        setupView();
    }

    private void initData() {
        yuYueBean = getSerializableParam("yuyue", YuYueBean.class);
        status = getIntParam("status");
    }

    private void setupView() {
        String xx = "";
        txtName.setText(yuYueBean.bname);
        txtTime.setText(yuYueBean.start_time + "---" + yuYueBean.end_time.split(" ")[1]);
        Glide.with(this).load(yuYueBean.user_photo).asBitmap().into(ivAvatar);
        txtSex.setText(yuYueBean.sex);
        txtAge.setText(yuYueBean.age + "岁");
        txtPhone.setText("联系方式：" + yuYueBean.tel);
        txtBs.setText("病        史：" + yuYueBean.mh);
        if (!TextUtils.isEmpty(yuYueBean.eating_habits)) {
            xx += XXBean.eat[Integer.parseInt(yuYueBean.eating_habits) - 1];
        }
        if (!TextUtils.isEmpty(yuYueBean.smoke)) {
            xx += "," + XXBean.smoke[Integer.parseInt(yuYueBean.smoke) - 1];
        }
        if (!TextUtils.isEmpty(yuYueBean.drink)) {
            xx += "," + XXBean.drink[Integer.parseInt(yuYueBean.drink) - 1];
        }
        if (!TextUtils.isEmpty(yuYueBean.exercise_habits)) {
            xx += "," + XXBean.sport[Integer.parseInt(yuYueBean.exercise_habits) - 1];
        }
        txtXx.setText(xx);
        if (!TextUtils.isEmpty(yuYueBean.mh) && !yuYueBean.mh.equals("尚未填写")) {
            String arr[] = yuYueBean.mh.split(",");
            String mh = "";
            for (int i = 0; i < arr.length; i++) {
                if (Integer.parseInt(arr[i]) - 1 < BsBean.BS.length)
                    mh += BsBean.BS[Integer.parseInt(arr[i]) - 1] + ",";
            }
            if (!TextUtils.isEmpty(mh)) {
                mh = mh.substring(0, mh.length() - 1);
            }
            txtBs.setText("病        史：" + mh);
        } else {
            txtBs.setText("病        史：尚未填写");
        }
        if (status == 1)
            layoutBottom.setVisibility(View.GONE);
    }

    @OnClick({R.id.txt_jj, R.id.txt_js})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_jj:
                commit(4);
                break;
            case R.id.txt_js:
                commit(1);
                break;
        }
    }

    public void commit(int state) {
        Map<String, String> map = new HashMap<>();
        map.put("rid", yuYueBean.rid + "");
        map.put("state", state + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "bl/app_update_reserve_state", map, new TypeToken<List<PatientBean>>() {
        }.getType(), new NetworkManager.SuccessCallback<List<PatientBean>>() {
            @Override
            public void onSuccess(List<PatientBean> response) {
                ToastUtil.showShort(SubscribeActivity.this, "成功");
                setResult(RESULT_OK);
                finish();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(SubscribeActivity.this, message);
            }
        });
    }

}
